//
//  CM.swift
//  TTCOM
//
//  Created by Toj on 6/10/22.
//

import Foundation
import QuartzCore

// 毫秒: Milliseconds, MS/ ms
// 秒  : Second, S/ s
// 分  : Minute, M/ m
// 时  : Hour, H/ h
// 天  : Day, D/ d

// 高: High, 低: Wow
// e.g.:
// 高 转 低: 天 to 时
// 低 转 高: 时 to 天

/// 毫秒
// 秒 与 毫秒 转换
public let CMSToMS: Int64 = 1000

/// 分
// 分 与 秒 转换
public let CMMToS: Int64 = 60
// 分 与 毫秒 转换
public let CMMToMS: Int64 = 60 * 1000

/// 时
// 时 与 分 转换
public let CMHToM: Int64 = 60
// 时 与 秒 转换
public let CMHToS: Int64 = 60 * 60
// 时 与 毫秒 转换
public let CMHToMS: Int64 = 60 * 60 * 1000

/// 天
// 天 与 时 转换
public let CMDToH: Int64 = 24
// 天 与 分 转换
public let CMDToM: Int64 = 24 * 60
// 天 与 秒 转换
public let CMDToS: Int64 = 24 * 60 * 60
// 天 与 毫秒 转换
public let CMDToMS: Int64 = 24 * 60 * 60 * 1000

private let objc_dictionary_key = "objc_dictionary_key"
public struct CM {
    
    public static func absoluteTimes(handler: (()->Void)) {
        let start = CFAbsoluteTimeGetCurrent()
        handler()
        let end = CFAbsoluteTimeGetCurrent()
        let dif = (end - start) * 1000
        print("-->com: exec start: \(start), end: \(end), diff:\(dif) ms")
    }
    
    public static func absoluteDiffTimes(handler: (()->Void)) {
        let start = CFAbsoluteTimeGetCurrent()
        handler()
        let end = CFAbsoluteTimeGetCurrent()
        let dif = (end - start) * 1000
        print("-->com: exec diff:\(dif) ms")
    }
    
    public static func absoluteMediaTimes(handler: (()->Void)) {
        let start = CACurrentMediaTime()
        handler()
        let end = CACurrentMediaTime()
        let dif = (end - start) * 1000
        print("-->com: exec start: \(start), end: \(end), diff:\(dif) ms")
    }
    
    public static func absoluteDiffMediaTimes(handler: (()->Void)) {
        let start = CACurrentMediaTime()
        handler()
        let end = CACurrentMediaTime()
        let dif = (end - start) * 1000
        print("-->com: exec diff:\(dif) ms")
    }
    
    static private var dictionaries: [String: Any] = [:]
    public static func begin(time key: String) {
        objc_sync_enter(self)
        dictionaries[key] = CACurrentMediaTime()
        objc_sync_exit(self)
    }
    
    public static func end(time key: String) -> Double {
        objc_sync_enter(self)
        let start = dictionaries[key] as? Double ?? 0
        dictionaries.removeValue(forKey: key)
        objc_sync_exit(self)
        let end = CACurrentMediaTime()
        
        guard start > 0 else { return 0 }
        let diff = (end - start)
        print("-->toj: \(key), diff: \(diff)")
        return diff
    }

    private static var globalDictionaries: [String: Any] = [:]
    public static func set(value: Any?, for key: String) {
        objc_sync_enter(self)
        globalDictionaries[key] = value
        objc_sync_exit(self)
    }
    
    public static func value(for key: String) -> Any? {
        objc_sync_enter(self)
        let beginTime = globalDictionaries[key]
        objc_sync_exit(self)
        return beginTime
    }
}

public enum CMLineStyle {
    case normal // 无
    case strikethrough // 中划线 
    case underline// 下划线
}

public enum STEnvironment {
    case sandbox
    case release
}


//public struct CMExtension<CMType> {
//    public let cself: CMType
//    public init(_ type: CMType) {
//        cself = type
//    }
//}
//
//public protocol CMExtended { }
//extension CMExtended {
//    public var cm: CMExtension<Self> {
//        get { CMExtension(self) }
//    }
//    public static var cm: CMExtension<Self>.Type {
//        get { CMExtension<Self>.self }
//    }
//}
